import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 666
 * Date: 2023-04-13
 * Time: 22:00
 */
public class io {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查询的目录->");
        String str = scanner.next();
        File file = new File(str);
        if(!file.isDirectory()){
            System.out.println("输入目录错误");
            return;
        }
        System.out.println("请输入要查询的关键词");
        String word = scanner.next();
        scanFile(file,word);
    }

    private static void scanFile(File file, String word) {
        File[] files = file.listFiles();
        if(files==null){
            return;
        }
        for (File f:files
             ) {
         //   System.out.println("当前找到了"+f.getAbsolutePath());
           if(f.isFile()){
               String  str1 = fileRead(f);
               if(str1.contains(word)){
                   System.out.println("找到了在"+f.getAbsolutePath());
               }
           }else if (f.isDirectory()){
               scanFile(f,word);
           }else {
               continue;
            }
        }

    }

    private static String fileRead(File f) {
        StringBuilder stringBuilder = new StringBuilder();

            try (Reader reader = new FileReader(f)){
while (true){
    int a= reader.read();
    if(a==-1){
        break;
    }
    stringBuilder.append((char)a);
}
            } catch (IOException e) {
                e.printStackTrace();
            }

        return stringBuilder.toString();
    }
}
